/**
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.community;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.album.AlbumHelper;
import com.tstudy.laoshibang.album.Bimp;
import com.tstudy.laoshibang.album.BitmapCache;
import com.tstudy.laoshibang.album.ImageBucket;
import com.tstudy.laoshibang.album.ImageGridAdapter;
import com.tstudy.laoshibang.album.ImageGridAdapter.TextCallback;
import com.tstudy.laoshibang.album.ImageItem;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.community.AlbumListAdapter.AdapterCallBack;
import com.tstudy.laoshibang.event.SelectPictureEvent;
import com.tstudy.laoshibang.login.NeedLoginFragment;
import com.tstudy.laoshibang.mode.Idxe;
import com.tstudy.laoshibang.utils.LogUtil;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
@SuppressLint("InflateParams")
@EFragment(R.layout.album)
public class AlbumFragment extends NeedLoginFragment{
	final static String TAG = "album";
	public AlbumFragment() {
		super();
	}
	
	@ViewById(R.id.album_root)
	RelativeLayout mRootLayout;
	
	
	@ViewById(R.id.album_finish)
	TextView mFinishAction;

	@ViewById(R.id.album_select)
	TextView mSelectTxt;
	
	@ViewById(R.id.album_grid_view)
	GridView mGridView;
	@StringRes(R.string.album_select_finish)
	String mFinishStr;
	
	boolean mIsFirstLoad = true;
	Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.arg1 == 0) {
				BaseApplication.showToast("最多选9张");
			}
		};
	};
	LayoutInflater mInflater;
	ImageGridAdapter mImageGridAdapter;
	List<ImageItem> mImages = new ArrayList<ImageItem>();
	List<Idxe> mIdxes = new ArrayList<Idxe>();
	
	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,AlbumFragment_.class.getName(),args);
		((BaseActivity)BaseApplication.mCurrentActivity).add(containerViewId,fragment,TAG);	
	
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		AlbumHelper.getHelper().init(BaseApplication.mContext);
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			mImages = AlbumHelper.getHelper().getImagesAllList(true);
			mImageGridAdapter = new ImageGridAdapter(mInflater, mImages, mHandler);
			mFinishAction.setText(String.format(mFinishStr,Bimp.drr.size(),CONSTANT.MAX_IMAGE_SELECT));
			mImageGridAdapter.setTextCallback(new TextCallback() {
				@Override
				public void onListen(int count) {
					mFinishAction.setText(String.format(mFinishStr,Bimp.drr.size()+count,CONSTANT.MAX_IMAGE_SELECT));
				}
			});
			mGridView.setAdapter(mImageGridAdapter);
//			mAdapter.notifyDataSetChanged();
		}
	}
	@Click({R.id.album_finish,R.id.album_title_back,R.id.album_select})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.album_title_back:
			clearSelect();
			backAction(mFragmentId);
			break;
		case R.id.album_select:
			showAlbumDialog();
			break;
		case R.id.album_finish:
			finish();
			backAction(mFragmentId);
			break;
		default:
			break;
		}
	}
	
	Dialog mAlbumDialog;
	int mSelectPosition = 0;
	BitmapCache mCache = new BitmapCache();
	@SuppressLint("InflateParams")
	public void showAlbumDialog(){
		RelativeLayout albumLayout = (RelativeLayout) mInflater.inflate(R.layout.album_pop, null);
		if(mAlbumDialog != null && mAlbumDialog.isShowing()){
			return;
		}
		mAlbumDialog = new Dialog(BaseApplication.mCurrentActivity, R.style.fullscreendialog);
		mAlbumDialog.setCanceledOnTouchOutside(true);
		mAlbumDialog.setContentView(albumLayout);
		ListView listView = (ListView) albumLayout.findViewById(R.id.album_pop_list);
		AlbumListAdapter adapter = new AlbumListAdapter(mCache);
		final List<ImageBucket> bucketList = AlbumHelper.getHelper().getImagesBucketList(false,mSelectPosition);
		adapter.setData(bucketList);
		adapter.setAdatperCallBack(new AdapterCallBack() {
			@Override
			public void onItemClick(ImageBucket bucket, int position) {
				bucketList.get(mSelectPosition).isSelected = false;
				bucketList.get(position).isSelected = true;
				mSelectPosition = position;
				mImageGridAdapter.setData(bucket.imageList);
				mSelectTxt.setText(bucket.bucketName);
				mAlbumDialog.dismiss();
			}
		});
		listView.setAdapter(adapter);
		Window window = mAlbumDialog.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(0x00000000));
		window.setLayout(BaseApplication.mScreenWidth,BaseApplication.mScreenHeight*2/3);
		window.setGravity(Gravity.BOTTOM);
		window.setWindowAnimations(R.style.dialog_from_bottom);
		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
				| WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		mAlbumDialog.show();
	}
	
	public void finish(){
		ArrayList<String> list = new ArrayList<String>();
		Collection<String> c = mImageGridAdapter.map.values();
		Iterator<String> it = c.iterator();
		for (; it.hasNext();) {
			list.add(it.next());
		}
		if (Bimp.act_bool) {
//			Intent intent = new Intent(ImageGridActivity.this,
//					PublishedActivity.class);
//			startActivity(intent);
			Bimp.act_bool = false;
		}
		for (int i = 0; i < list.size(); i++) {
			if (Bimp.drr.size() < 9) {
				Bimp.drr.add(list.get(i));
			}
		}
		SelectPictureEvent event = new SelectPictureEvent();
		event.filePaths = Bimp.drr;
//		mAdapter.map.clear();
		EventBus.getDefault().post(event);
	}
	
	public void clearSelect(){
		mImageGridAdapter.clearSelect();
	}
	
	@Override
	public void clear() {
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	};
	
}
